import { DatePicker } from '@/components/AntdProxy';
import Crud from '@/components/Crud';
import {
  DriverSelect,
  GroupTreeSelect,
  LineSelect,
} from '@/components/CustomForm';

import { formatSeconds } from '@/utils/format';
import request from '@/utils/request';
import { DownloadOutlined } from '@ant-design/icons';
import { Button } from 'antd';
import dayjs from 'dayjs';
import { useRef } from 'react';

const getList = async (params) => {
  const res = await request({
    url: '/datas/summary/driver/trip/group/line/list',
    method: 'GET',
    custom: true,
    params: params,
  });
  return {
    total: res?.total,
    data: res?.data,
  };
};

const Index = (props) => {
  const { onDetail } = props;
  const crudRef = useRef();

  const columns = [
    {
      title: '序号',
      fixed: 'left',
      width: 65,
      render: (text, record, index) => index + 1,
    },
    {
      title: '组织',
      dataIndex: 'groupName',
      fixed: 'left',
      width: 220,
    },
    {
      title: '线路',
      dataIndex: 'lineName',
      fixed: 'left',
      width: 280,
    },
    {
      title: '姓名',
      dataIndex: 'realname',
      fixed: 'left',
      width: 120,
    },
    {
      title: '手机号',
      dataIndex: 'phone',
      width: 120,
    },
    {
      title: '趟次数',
      dataIndex: 'trips',
      width: 100,
      render: (text, record) => (
        <a
          title="点击查看明细"
          onClick={() => {
            const params = crudRef.current?.getParams();
            onDetail?.({
              lineId: record.lineId,
              driverId: record.driverId,
              fromTime: dayjs(params.fromTime).startOf('day'),
              toTime: dayjs(params.toTime).endOf('day'),
              uuid: dayjs().valueOf(),
            });
          }}
        >
          {text}
        </a>
      ),
    },
    {
      title: '总时长',
      dataIndex: 'duration',
      width: 100,
      render: (text) => formatSeconds(text),
    },
    {
      title: '平均时长',
      dataIndex: 'duration',
      width: 100,
      render: (text, record) => {
        return record.trips > 0
          ? formatSeconds(Math.round(text / record.trips))
          : '0';
      },
    },
    {
      title: '总里程(km)',
      dataIndex: 'mileages',
      width: 110,
    },
  ];

  const fields = [
    {
      label: '组织',
      name: 'groupId',
      children: <GroupTreeSelect placeholder="请选择组织" />,
    },
    {
      label: '线路',
      name: 'lineId',
      children: <LineSelect placeholder="请选择线路" />,
    },
    {
      label: '驾驶员',
      name: 'driverId',
      children: <DriverSelect placeholder="请选择驾驶员" />,
    },
    {
      label: '开始日期',
      name: 'fromTime',
      children: (
        <DatePicker
          allowClear={false}
          placeholder="请选择开始日期"
          format="YYYY-MM-DD"
        />
      ),
    },
    {
      label: '结束日期',
      name: 'toTime',
      children: (
        <DatePicker
          allowClear={false}
          placeholder="请选择结束日期"
          format="YYYY-MM-DD"
        />
      ),
    },
  ];

  const handleExport = async () => {
    const params = crudRef.current?.getParams();
    const res = await request({
      url: '/datas/summary/driver/trip/group/line/export/list',
      method: 'GET',
      params,
    });
    window.open(res);
  };

  return (
    <>
      <Crud
        ref={crudRef}
        rowKey="id"
        queryFormProps={{
          labelCol: 7,
          initialValues: {
            fromTime: dayjs().subtract(30, 'day').startOf('day'),
            toTime: dayjs().subtract(1, 'day').endOf('day'),
          },
        }}
        fields={fields}
        columns={columns}
        request={getList}
        transformParams={(params) => {
          return {
            ...params,
            fromTime: params?.fromTime?.format('YYYY-MM-DD'),
            toTime: params?.toTime?.format('YYYY-MM-DD'),
          };
        }}
        toolbar={
          <>
            <Button
              type="primary"
              icon={<DownloadOutlined />}
              onClick={handleExport}
            >
              导出
            </Button>
          </>
        }
      />
    </>
  );
};

export default Index;
